﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using General.Framework.Controllers.Admin;
using General.Services.Category;
using General.Framework.Menu;
using System.IO;
using General.Core;
using Microsoft.Extensions.Caching.Memory;
using General.Framework.Filters;

namespace General.Mvc.Areas.Admin.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("admin/category")]
    [NotPermissionFilter]
    public class CategoryController : AdminPermissionController
    {
        private ICategoryService _categoryService;

        public CategoryController(ICategoryService categoryService)
        { 
            this._categoryService = categoryService;
        }

        /// <summary>
        /// 功能菜单列表
        /// </summary>
        /// <returns></returns>
        [Route("", Name = "categoryIndex")]
        public IActionResult CategoryIndex()
        {
            var xmlSiteMap = new XmlSiteMap();
            xmlSiteMap.LoadFrom(Path.Combine(Directory.GetCurrentDirectory(), "sitemap.xml"));
            List<Entities.Category> list = new List<Entities.Category>();
            xmlSiteMap.SiteMapNodes.ForEach(item =>
            {
                list.Add(new Entities.Category()
                {
                    Action = item.Action ?? "",
                    Controller = item.Controller ?? "",
                    CssClass = item.IconClass ?? "",
                    IsMenu = item.IsMenu,
                    Name = item.Name,
                    RouteName = item.RouteName,
                    SysResource = item.SysResource,
                    Sort = item.Sort,
                    FatherID = item.FatherID ?? "",
                    ResouceID = item.ResouceID

                });
            });
            return View(list);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        [Route("init",Name = "initCategory")]
        public IActionResult InitCategory()
        { 
            _categoryService.InitCategory();
            AjaxData.Status = true;
            AjaxData.Message = "初始化成功";
            return Json(AjaxData);
        }

    }
}